package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.CarCanHandlerEntity
import com.c.ctgproject.ui.collect.CarCanHandlerListActivity
import com.c.ctgproject.ui.collect.SearchCarInfoActivity
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.startActivity

class CarCanHandlerAdapter(private val context: Context, private val mList: List<CarCanHandlerEntity.DataBean.ListBean>,private val mlistener:OnCheckBoxClickedListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_car_can_handler_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            val type = when (data.carType) {
                1 -> "社会车"
                2 -> "试验车"
                3 -> "外包车"
                4 -> "五类车"
                5 -> "农机具"
                else -> "暂无数据"
            }
            holder.mCarType.text = type


            val carStatus = when (data.carStatus) {
                1 -> "整车"
                2 -> "车架"
                else -> "暂无数据"
            }
            holder.mCarStatueTV.text = carStatus


            holder.mUserNameTV.text = data.createPersonName.noDataHandler()
            val energy = when (data.energy) {
                1 -> "燃油车"
                2 -> "电动车"
                3 -> "油电混合"
                else -> "暂无数据"
            }
            holder.mBatteryModelTV.text = energy
            holder.carPlate.text = data.carPlate.noDataHandler()
            holder.mFrom.text = data.carSource.noDataHandler()
            holder.mVIN.text = data.carVin.noDataHandler()
            holder.mCarCodeTV.text = data.carCode.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.mCheckBox.isChecked = data.select
            holder.mCheckBox.setOnClickListener {
                mList[position].select = holder.mCheckBox.isChecked
                mlistener.OnItemCheckBoxClicked()
                notifyItemChanged(position)
            }
            holder.mParentLL.setOnClickListener {
//                context.startActivity<CarInfoActivity>(MyConstants.CAR_ID to data.carId)
                context.startActivity<SearchCarInfoActivity>(MyConstants.CAR_ID to data.carId)
            }
            val ac=context as CarCanHandlerListActivity

            holder.mCheckBox.visibility =if (ac.searchMine) View.INVISIBLE else View.VISIBLE


            //是否已拆解 拆解 0-未完成 1-已完成 2-拆解中 改为 【1-拆解中 2-已完成】
//            holder.mCheckBox.isEnabled = data.dismantle == 1


        }
    }
    interface OnCheckBoxClickedListener{
        fun OnItemCheckBoxClicked()
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val carPlate: TextView = view.findViewById(R.id.carPlate)
        val mCarCodeTV: TextView = view.findViewById(R.id.mCarCodeTV)
        val mFrom: TextView = view.findViewById(R.id.mFrom)
        val mUserNameTV: TextView = view.findViewById(R.id.mUserNameTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mVIN: TextView = view.findViewById(R.id.mVIN)
        val mCarType: TextView = view.findViewById(R.id.mCarType)
        val mBatteryModelTV: TextView = view.findViewById(R.id.mBatteryModelTV)
        val mCarStatueTV: TextView = view.findViewById(R.id.mCarStatueTV)
        val mCheckBox: CheckBox = view.findViewById(R.id.mCheckBox)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
    }


}